<!-- Area 表单弹窗 -->
<form id="area-form" lay-filter="area-form" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">上级区域</label>
        <div class="layui-input-block">
            <select name="parentId">
                <option value="-1"></option>
                <option th:each="area : ${areas}" th:value="${area.id}" th:text="${area.name}"></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">区域编码</label>
        <div class="layui-input-block">
            <input name="code" placeholder="请输入区域编码" type="text" class="layui-input" maxlength="32"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">区域名称</label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入区域名称" type="text" class="layui-input" maxlength="128"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">区域类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="required" required>
                <option></option>
                <option value="1">国家</option>
                <option value="2">省份、直辖市</option>
                <option value="3">地市</option>
                <option value="4">区县</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input name="sort" placeholder="请输入排序号" type="number" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" type="button" app-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="area-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;

        form.render('select');

        var url = 'organization/area/add';
        // 回显area数据
        var area = admin.getTempData('t_area');
        form.val('area-form', area);
        if (area && area.id) {
            url = 'organization/area/update';
        }

        // 表单提交事件
        form.on('submit(area-form-submit)', function (data) {
            layer.load(2);
            $.post(url, data.field, function (data) {
                layer.closeAll('loading');
                if (data.success) {
                    layer.msg(data.msg, {icon: 1});
                    admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            });
            return false;
        });
    });
</script>